// 引入axios 
import axios from 'axios'
//引入qs
import qs from 'qs'
import Vue from 'vue'
// 配置api的基础路径,所有通过axios发起的请求都会以该路径为基础
axios.defaults.baseURL = process.env.VUE_APP_ROOTAPI;
//请求的拦截器
axios.interceptors.request.use(function(config){
    const token = localStorage.getItem("token");
    config.headers.common['Authorization'] = 'Bearer '+token;
    return config;
},function (error){
    return Promise.reject(error);
});
// console.log(axios.defaults);
// console.log(process);

//响应拦截器
axios.interceptors.response.use(function (response){
    return response;
},function(error){
    return Promise.reject(error);
});

var myAxios = {
    get:(url,params)=>{
        return axios.get(url,{params:params})
        .then((resp)=>{
            return resp;
        })
    },
    post:(url,params,config)=>{
        return axios.post(url,qs.stringify(params),config)
        .then((resp)=>{
            return resp;
        })
    }
}

Vue.prototype.$http = myAxios;
//导出一个对象
export default myAxios;